home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / Crossword / Source / AppDelegate.m < prev    next >
Text File  |  1992-10-12  |  4KB  |  219 lines

  1. /*
  2.  
  3. File AppDelegate.m
  4.  
  5. The application delegate is responsible for updating menus and handling basic menu commands.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10.  
  11. #import "AppDelegate.h"
  12. #import "WinDelegate.h"
  13. #import "Crossword.h"
  14. #import "Inspector.h"
  15. #import "filing.h"
  16.  
  17.  
  18. /* ————————————————————————————————————————————————————————————————————————————  */
  19.  
  20.  
  21. #define    PLAINMENU        0
  22. #define    PUZZLEMENU        1
  23. #define NUMBERMENU        2
  24. #define CONTINUEMENU    3
  25.  
  26. static BOOL                isNumbered = NO;
  27. static const char *        numberMenuTitle [] = {
  28.  
  29.         "Number",
  30.         "Unnumber"
  31.     };
  32.  
  33. static char *    types [] = {
  34.  
  35.         "xword",
  36.         "XWORD",    NULL
  37.     };
  38.  
  39. static const char *    pasteTypes [] = {
  40.  
  41.         NULL,
  42.         NULL
  43.     };
  44.  
  45. static void        initMenu    (id);
  46.  
  47.  
  48. /* ————————————————————————————————————————————————————————————————————————————  */
  49.  
  50.  
  51. @implementation AppDelegate
  52.  
  53.  
  54. - appDidInit: sender
  55. {
  56.     initMenu([NXApp  mainMenu]);
  57.     [NXApp  setAutoupdate: YES];
  58.  
  59.     return self;
  60. }
  61.  
  62.  
  63. - (BOOL) appAcceptsAnotherFile: sender
  64. {
  65.     return YES;
  66. }
  67.  
  68.  
  69. - (BOOL) app: sender  openFile: (const char *) file  type: (const char *) type
  70. {
  71.     [NXApp  loadNibSection: "Puzzle.nib"  owner: inspector];
  72.     [NXGetNamedObject("Puzzle", inspector)  readPuzzle: file];
  73.     
  74.     return YES;
  75. }
  76.  
  77.  
  78. /* ————————————————————————————————————————————————————————————————————————————  */
  79.  
  80.  
  81. - new: sender
  82. {
  83.     [NXApp  loadNibSection: "Puzzle.nib"  owner: inspector];
  84.     [NXGetNamedObject("Puzzle", inspector)  new];
  85.  
  86.     return self;
  87. }
  88.  
  89.  
  90. - open: sender
  91. {
  92.     const char    * filename;
  93.     
  94.     if ((filename = fileForOpen(types)) != NULL)
  95.     {
  96.         [NXApp  loadNibSection: "Puzzle.nib"  owner: inspector];
  97.         [NXGetNamedObject("Puzzle", inspector)  readPuzzle: filename];
  98.     }
  99.     
  100.     return self;
  101. }
  102.  
  103.  
  104. - copyTime: sender
  105. {
  106.     id            pasteboard;
  107.     const char    * value;
  108.     
  109.     pasteboard = [Pasteboard  new];
  110.     value = [[inspector  getTimeCell]  stringValue];
  111.     pasteTypes[0] = NXAsciiPboardType;
  112.     
  113.     while (*value == ' ') value++;
  114.     
  115.     [pasteboard
  116.             declareTypes: pasteTypes
  117.             num: 1
  118.             owner: nil];
  119.     
  120.     [pasteboard
  121.             writeType: NXAsciiPboardType
  122.             data: value
  123.             length: strlen(value)];
  124.     
  125.     return self;
  126. }
  127.  
  128.  
  129. /* ————————————————————————————————————————————————————————————————————————————  */
  130.  
  131.  
  132. - (BOOL) menuItemUpdate: menuCell
  133. {
  134.     id        puzzle;
  135.     BOOL    status, update;
  136.     
  137.     switch ([menuCell  tag])
  138.     {
  139.         case PUZZLEMENU:
  140.             if ((status = ([self  mainCrossword] != nil)) != [menuCell  isEnabled])
  141.             {
  142.                 [menuCell  setEnabled: status];
  143.                 return YES;
  144.             }
  145.             
  146.             else return NO;
  147.             break;
  148.             
  149.         case NUMBERMENU:
  150.             update = NO;
  151.             
  152.             if (((puzzle = [self  mainCrossword]) != nil) != [menuCell  isEnabled])
  153.             {
  154.                 [menuCell  setEnabled: puzzle != nil];
  155.                 update = YES;
  156.             }
  157.             
  158.             if ([puzzle  getNumbered] != isNumbered)
  159.             {
  160.                 [menuCell  setTitleNoCopy:
  161.                                 numberMenuTitle[isNumbered = !isNumbered]];
  162.                 update = YES;
  163.             }
  164.             
  165.             return update;
  166.             break;
  167.             
  168.         case CONTINUEMENU:
  169.             if ((status = [[self  mainPuzzle]  canContinue])
  170.                                         != [menuCell  isEnabled])
  171.             {
  172.                 [menuCell  setEnabled: status];
  173.                 return YES;
  174.             }
  175.             
  176.             else return NO;
  177.             break;            
  178.             
  179.         default:
  180.             return NO;
  181.             break;
  182.     }
  183. }
  184.  
  185.  
  186. - mainCrossword        {    return [[NXApp  mainWindow]  firstResponder];    }
  187. - mainPuzzle        {    return [[NXApp  mainWindow]  delegate];            }
  188.  
  189.  
  190. @end
  191.  
  192.  
  193. /* ————————————————————————————————————————————————————————————————————————————  */
  194.  
  195.  
  196. /*
  197.  
  198. Here is code for updating the menu items.  Some menus need a current scene.  Others need a first responder.  The code is taken from NeXTanswers appkit #636.
  199.  
  200. */
  201.  
  202. static void initMenu (id menu)
  203. {
  204.     int    i;
  205.     id    matrix, cell;
  206.  
  207.     matrix = [menu  itemList];
  208.     i = [matrix  cellCount];
  209.     
  210.     while (i--)
  211.     {
  212.         cell = [matrix  cellAt: i:0];
  213.         if ([cell  tag] != PLAINMENU)
  214.                 [cell  setUpdateAction: @selector(menuItemUpdate:)  forMenu: menu];
  215.         
  216.         else if ([cell hasSubmenu])
  217.                 initMenu([cell  target]);
  218.     }
  219. }